package org.yzz.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.yzz.auth.dto.LoginDTO;
import org.yzz.auth.service.ILoginService;
import org.yzz.base.utils.AjaxResult;
import org.yzz.base.utils.LoginContext;

import javax.servlet.http.HttpServletRequest;


@RestController
public class LoginController {
    @Autowired
    private ILoginService service;

    @PostMapping("/Login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        try {
            return AjaxResult.success(service.login(loginDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/LoginOut")
    public AjaxResult loginOut(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            LoginContext.map.remove(token);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
